package simulator.ui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Hashtable;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;

/**
 * Basic modal dialog for allowing cycles per second to be specified through the
 * UI.
 * 
 * @author Kyle Loomis
 * 
 */
public class PropertiesDialog extends JDialog implements ActionListener
{
   public static final long serialVersionUID = 1L;
   private JFrame parent = null;
   private SimulationController controller = null;
   private JButton okButton = null;
   private JButton cancelButton = null;
   private JSlider cpsSlider = null;

   /**
    * Default controller.
    * 
    * @param controller
    * @param parent
    */
   public PropertiesDialog(SimulationController controller, JFrame parent)
   {
      super(parent, true);

      this.controller = controller;
      this.parent = parent;

      setTitle("Properties");
      setLayout(new GridBagLayout());

      JPanel buttonPanel = new JPanel();
      setSize(400, 125);

      buttonPanel.setVisible(true);

      JPanel controlPanel = new JPanel();
      controlPanel.setVisible(true);

      JLabel cpsLabel = new JLabel("Cycles per second");
      controlPanel.add(cpsLabel);

      cpsSlider = new JSlider(1, 11);
      cpsSlider.setValue(translate(controller.getDelay()));

      Dictionary<Integer, JComponent> labels = new Hashtable<Integer, JComponent>();
      labels.put(new Integer(11), new JLabel("20"));
      labels.put(new Integer(10), new JLabel("10"));
      labels.put(new Integer(9), new JLabel("5"));
      labels.put(new Integer(5), new JLabel("1"));
      labels.put(new Integer(1), new JLabel(".2"));

      cpsSlider.setLabelTable(labels);
      cpsSlider.setMajorTickSpacing(1);
      cpsSlider.setSnapToTicks(true);
      cpsSlider.setMinorTickSpacing(1);
      cpsSlider.setPaintLabels(true);
      cpsSlider.setPaintTicks(true);
      controlPanel.add(cpsSlider);
      okButton = new JButton("OK");
      cancelButton = new JButton("Cancel");
      okButton.setPreferredSize(new Dimension(80, 24));
      cancelButton.setPreferredSize(new Dimension(80, 24));
      okButton.addActionListener(this);
      cancelButton.addActionListener(this);

      HashMap<String, JComponent> controls = new HashMap<String, JComponent>();
      controls.put("cps", cpsSlider);

      buttonPanel.add(okButton);
      buttonPanel.add(cancelButton);

      GridBagConstraints controlsConstraints = new GridBagConstraints();
      controlsConstraints.gridheight = 2;
      controlsConstraints.weightx = 1;
      controlsConstraints.weighty = 1;
      controlsConstraints.gridy = 0;
      add(controlPanel, controlsConstraints);

      GridBagConstraints buttonConstraints = new GridBagConstraints();
      buttonConstraints.anchor = GridBagConstraints.LINE_END;
      buttonConstraints.gridy = 2;
      buttonConstraints.gridheight = 1;
      buttonConstraints.weightx = 1;
      buttonConstraints.weighty = 0;
      add(buttonPanel, buttonConstraints);

      setVisible(true);
   }

   private int translate(int delay)
   {
      int cps = 1;

      switch (delay)
      {
      case 5000:
         cps = 1;
         break;
      case 4000:
         cps = 2;
         break;
      case 3000:
         cps = 3;
         break;
      case 2000:
         cps = 4;
         break;
      case 1000:
         cps = 5;
         break;
      case 800:
         cps = 6;
         break;
      case 600:
         cps = 7;
         break;
      case 400:
         cps = 8;
         break;
      case 200:
         cps = 9;
         break;
      case 100:
         cps = 10;
         break;
      case 50:
         cps = 11;
         break;
      }

      return cps;
   }

   public void actionPerformed(ActionEvent e)
   {
      if (e.getSource() == okButton)
      {
         int cps = cpsSlider.getValue();
         int delay = 1000;

         switch (cps)
         {
         case 1:
            delay = 5000;
            break;
         case 2:
            delay = 4000;
            break;
         case 3:
            delay = 3000;
            break;
         case 4:
            delay = 2000;
            break;
         case 5:
            delay = 1000;
            break;
         case 6:
            delay = 800;
            break;
         case 7:
            delay = 600;
            break;
         case 8:
            delay = 400;
            break;
         case 9:
            delay = 200;
            break;
         case 10:
            delay = 100;
            break;
         case 11:
            delay = 50;
            break;
         }

         if (controller.getStatus() == SimulationController.STATUS_RUNNING)
         {
            controller.pause();
            controller.setDelay(delay);
            controller.start();
         }
         else
         {
            controller.setDelay(delay);
         }

         dispose();
      }
      else
      {
         dispose();
      }
   }
}
